package ytmc.setup;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JComboBox;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import ytmc.PanelView;


/**
 *
 * @author Kicok
 */
public class SetupView extends PanelView implements Observer
{
    private SetupModel model;
    private SetupController controller;

    private boolean isFormChanged;


    public SetupView( SetupController controller, SetupModel model )
    {
        this.model = model;
        this.model.addObserver( this );
        this.controller = controller;

        initComponents();
        initComboBoxes();
        createListeners();

        fillForm();
    }

    private void initComboBoxes()
    {
        initVideoFormatComboBox();
        initVideoResolutionComboBox();
        initAudioFormatComboBox();
        initAudioQualityComboBox();
    }

    private void initVideoFormatComboBox( )
    {
        videoFormatComboBox.removeAllItems();

        for( String extension : VideoFormat.friendlyNames() )
        {
            videoFormatComboBox.addItem( "." + extension );
        }
    }

    private void initVideoResolutionComboBox()
    {
        videoResolutionComboBox.removeAllItems();

        for( String resolution : VideoResolution.friendlyNames() )
        {
            videoResolutionComboBox.addItem( resolution );
        }
    }

    private void initAudioFormatComboBox()
    {
        audioFormatComboBox.removeAllItems();

        for( String extension : AudioFormat.friendlyNames() )
        {
            audioFormatComboBox.addItem( "." + extension );
        }
    }

    private void initAudioQualityComboBox()
    {
        audioQualityComboBox.removeAllItems();

        for( String quality : AudioQuality.friendlyNames() )
        {
            audioQualityComboBox.addItem( quality );
        }
    }


    private void createListeners()
    {
        loginField.getDocument().addDocumentListener( new FormChangedDocumentListener() );
        passwordField.getDocument().addDocumentListener( new FormChangedDocumentListener() );

        videoFormatComboBox.addItemListener( new FormChangedItemListener() );
        videoResolutionComboBox.addItemListener( new FormChangedItemListener() );
        videoFrameRateSpinner.addChangeListener( new FormChangedChangeListener() );

        audioFormatComboBox.addItemListener( new FormChangedItemListener() );
        audioFormatComboBox.addItemListener( new DisableAQItemListener( this.model ) );
        audioQualityComboBox.addItemListener( new FormChangedItemListener() );
    }


    private void fillForm()
    {
        loginField.setText( model.getLogin() );
        passwordField.setText( model.getPassword() );

        videoFormatComboBox.setSelectedIndex( model.getVideoFormat().ordinal() );
        videoResolutionComboBox.setSelectedIndex( model.getVideoResolution().ordinal() );
        videoFrameRateSpinner.setValue( model.getVideoFrameRate() );

        audioFormatComboBox.setSelectedIndex( model.getAudioFormat().ordinal() );
        audioQualityComboBox.setSelectedIndex( model.getAudioQuality().ordinal() );
    }


    @Override
    public void update( Observable o, Object o1 )
    {
        if( ( (String)o1 ).equals( "form-changed" ) )
        {
            fillForm();
        }
    }


    public boolean isFormChanged()
    {
        return isFormChanged;
    }

    public void setFormChanged( boolean isFormChanged )
    {
        this.isFormChanged = isFormChanged;

        applyButton.setEnabled( isFormChanged );
        cancelButton.setEnabled( isFormChanged );
    }


    public void sendForm()
    {
        model.setLogin( loginField.getText() );
        model.setPassword( new String( passwordField.getPassword() ) );

        model.setVideoFormat( videoFormatComboBox.getSelectedIndex() );
        model.setVideoResolution( videoResolutionComboBox.getSelectedIndex() );
        model.setVideoFrameRate( ( (Integer)videoFrameRateSpinner.getValue() ).intValue() );

        model.setAudioFormat( audioFormatComboBox.getSelectedIndex() );
        model.setAudioQuality( audioQualityComboBox.getSelectedIndex() );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        loginField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        videoFormatComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        videoResolutionComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        videoFrameRateSpinner = new javax.swing.JSpinner();
        cancelButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        audioFormatComboBox = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        audioQualityComboBox = new javax.swing.JComboBox();

        setMinimumSize(new java.awt.Dimension(600, 460));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Konto YouTube"));

        jLabel1.setText("Login");

        jLabel2.setText("Hasło");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passwordField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(loginField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                .addContainerGap(306, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Konwersja wideo"));

        jLabel3.setText("Format wyjściowy");

        videoFormatComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ".3gp", ".avi", ".flv", ".mkv", ".mov", ".mp4", ".mpeg", ".wmv", " ", " " }));
        videoFormatComboBox.setSelectedIndex(1);

        jLabel4.setText("Rozdzielczość wyjściowa");

        videoResolutionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "jak oryginał", "570x320", "854x480", "1280x720", "1920x1080", "160x120", "240x160", "320x240", "480x320", "640x480", "800x600", "" }));

        jLabel5.setText("Ilość klatek na sekundę (0 = automatycznie)");

        videoFrameRateSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 60, 1));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(videoFormatComboBox, 0, 255, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(videoResolutionComboBox, 0, 255, Short.MAX_VALUE)
                    .addComponent(jLabel5)
                    .addComponent(videoFrameRateSpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(videoFormatComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(videoResolutionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(videoFrameRateSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        cancelButton.setText("Anuluj");
        cancelButton.setDefaultCapable(false);
        cancelButton.setEnabled(false);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        applyButton.setText("Zastosuj");
        applyButton.setEnabled(false);
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Konwersja audio"));

        jLabel6.setText("Format wyjściowy");

        audioFormatComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ".flac", ".mp3", ".wma" }));

        jLabel7.setText("Jakość");

        audioQualityComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "64 kbps", "96 kbps", "128 kbps", "160 kbps", "192 kbps", "240 kbps", "320 kbps" }));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, audioFormatComboBox, org.jdesktop.beansbinding.ELProperty.create("${selectedItem}"), audioQualityComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(audioFormatComboBox, 0, 259, Short.MAX_VALUE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(audioQualityComboBox, 0, 259, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(audioFormatComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(audioQualityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(108, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(applyButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(applyButton)
                    .addComponent(cancelButton))
                .addGap(23, 23, 23))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
    {//GEN-HEADEREND:event_cancelButtonActionPerformed
        controller.formCanceled();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_applyButtonActionPerformed
    {//GEN-HEADEREND:event_applyButtonActionPerformed
        controller.formAccepted();
    }//GEN-LAST:event_applyButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JComboBox audioFormatComboBox;
    private javax.swing.JComboBox audioQualityComboBox;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField loginField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JComboBox videoFormatComboBox;
    private javax.swing.JSpinner videoFrameRateSpinner;
    private javax.swing.JComboBox videoResolutionComboBox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables


    private class FormChangedDocumentListener implements DocumentListener
    {
        @Override
        public void insertUpdate( DocumentEvent de )
        {
            setFormChanged( true );
        }

        @Override
        public void removeUpdate( DocumentEvent de )
        {
            setFormChanged( true );
        }

        @Override
        public void changedUpdate( DocumentEvent de )
        {
            setFormChanged( true );
        }
    }

    private class FormChangedItemListener implements ItemListener
    {
        @Override
        public void itemStateChanged( ItemEvent ie )
        {
            setFormChanged( true );
        }
    }

    private class DisableAQItemListener implements ItemListener
    {
        private SetupModel model;

        public DisableAQItemListener( SetupModel model )
        {
            this.model = model;
        }


        @Override
        public void itemStateChanged( ItemEvent ie )
        {
            int index = ((JComboBox)ie.getSource()).getSelectedIndex();
            AudioFormat format = AudioFormat.get( index );

            audioQualityComboBox.setEnabled( format.hasAudioQualities() );
        }
    }

    private class FormChangedChangeListener implements ChangeListener
    {
        @Override
        public void stateChanged( ChangeEvent ce )
        {
            setFormChanged( true );
        }
    }
}
